/*
 * graph2D
 * Copyright (c) 2009 Shun Moriya <shun@mnu.sakura.ne.jp>
 *
 * The MIT License
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
 * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include "emitter.h"
#include "particle.h"
#include "scene.h"
#include <cassert>
#include <cstdlib>

namespace Graph2D
{
	Emitter::Emitter()
	{
	}

	Emitter::Emitter(Scene* scene)
	{
		assert(scene);

		this->scene = scene;
		this->texture = NULL;
		this->cellAnimation = NULL;
		this->life = 1.0f;
		this->enable = false;
		this->intervalWait = (1.0f / 100.0f);
	}

	Emitter::~Emitter()
	{
#if 0
		delete texture;
		texture = NULL;
#endif
		delete cellAnimation;
		cellAnimation = NULL;
	}

	void Emitter::onUpdate(const UpdateInfomation& updateInfomation)
	{
		ParticleManager* particleManager = scene->getParticleManager();

		if(enable)
		{
			life -= updateInfomation.deltaTime;
			if(life <= 0.0f)
				enable = false;
		}

		if(life > 0.0f)
		{
			intervalWait -= updateInfomation.deltaTime;
			if(intervalWait <= 0.0f)
			{
				do{
					Particle* particle = particleManager->popFreeParticle();
					if(particle)
					{
						const float RANDOM = static_cast<float>(rand() & 0x7FFF) / (32767.0f / 2.0f) - 1.0f;
						const float angle = 0.0f * (MANA_PI / 180.f);
						const float angleOffset = 25.0f * (MANA_PI / 180.f) * RANDOM;

						const float finalAngle = angle + angleOffset;
						const float initialSpeed = 88;

						const Vector2 velocity(sinf(finalAngle) * initialSpeed, -cosf(finalAngle) * initialSpeed);

						// パーティクルを初期化します
						particle->setGravity(Vector2(0.0f, 59.8f));
						particle->setVelocity(velocity);
						particle->setLife(2.0f);

						particle->setPosition(getPosition());
						particle->setSize(16.0f, 16.0f);
						particle->setColor(Color(1.0f, 1.0f, 1.0f, 1.0f), 0.0f);
						particle->setColor(Color(0.0f, 0.0f, 1.0f, 0.0f), 2.0f);
						particle->setTexture(getTexture());
#if 0
// TODO
						particle->addAnimation("particle", cellAnimation);
						particle->setAnimation("particle");
#endif
						particle->setVisible(true);

						scene->set(1000 + particle->getIndex(), particle);

						particleManager->activate(particle);
					}
					intervalWait += (1.0f / 100.0f);
				}while(intervalWait <= 0.0f);
				intervalWait = (1.0f / 100.0f);
			}
		}

		super::onUpdate(updateInfomation);
	}
}
